
/***  DOCUMENTATION LAYER

	Generic Javascript Class File
	* for use with class.ini.js
	
	Last Update: Apr 2007
	Author: Tom at klenwell@gmail.com
	License: GPL License (http://www.opensource.org/licenses/gpl-license.php)

FUNCTIONS	
	ini = function(af_name)
	_event_cache = function()
	attach_event = function(id, type, fx)
	cx_wrapper = function(fid)
	cx_textarea = function(ef_name, ef_label, required, cols, rows)
	cx_input_text = function(ef_name, ef_label, required, size)
	cx_input_checkbox = function(ef_name, ef_label, required, is_checked)
	cx_input_submit = function(ef_name, ef_label)
	cx_label = function(fid)
	toggle_display = function(fid)
	publish = function(pid)
	compress_html = function(str, preformatted)
	escape_html = function(html)
	unescape_html = function(html)

NOTES
	Base class should be defined in the class.ini.js file, which autoloads this
	file.  The class init method can then be called to initialize the object.  Other
	methods can be added to this file. 
	
	To link: <script type="text/javascript" src="path/to/file.js"></script>

REFERENCE
	http://klenwell.googlecode.com/svn/trunk/JS/jini/
	http://developer.mozilla.org/en/docs/JavaScript

______________________________________________________________________________*/


// method: ini
/*____________________________________________________________________________*/
AkwaForm.prototype.ini = function(af_name, is_pseudo)
{
	this.name = af_name;
	this.is_pseudo = ( is_pseudo != null ) ? is_pseudo : false;
	this.AF_IDX = new Object();  /* EF Field Index */
	this.FormObj = ( this.is_pseudo ) ? document.createElement('div') : document.createElement('form');
	this.FormObj.name = this.name;
	this.FormObj.id = this.name;

	// add unload event
	this.attach_event('window', 'unload', this._event_cache.flush);
	
	// debug
	if ( this.debug ) alert('new AkwaForm named ' + this.name + ' created as ' + this.FormObj);
};
/*____________________________________________________________________________*/


// method: event_cache
/*____________________________________________________________________________*/
AkwaForm.prototype._event_cache = function()
{
	var listEvents = [];
	
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler) { listEvents.push(arguments); },
		flush : function() 
		{
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1)
			{
				item = listEvents[i];
				if( item[0].removeEventListener ) { item[0].removeEventListener(item[1], item[2], item[3]); };
				if( item[1].substring(0, 2) != "on" ) { item[1] = "on" + item[1]; };
				if( item[0].detachEvent ) { item[0].detachEvent(item[1], item[2]); };
				item[0][item[1]] = null;
			};
		}
	};
}(); // end method
/*____________________________________________________________________________*/


// method: attach_event
/*____________________________________________________________________________*/
AkwaForm.prototype.attach_event = function(ef_name, type, fx)
{
	if ( ef_name == 'window' ) var obj = window;	
	else var obj = this[ef_name];

	if ( obj.addEventListener ) 
	{
		obj.addEventListener( type, fx, false );
		this._event_cache.add(obj, type, fx);
	}
	else if ( obj.attachEvent ) 
	{
		obj["e"+type+fx] = fx;
		obj[type+fx] = function() { obj["e"+type+fx]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fx] );
		this._event_cache.add(obj, type, fx);
	}
	else
	{
		obj["on"+type] = obj["e"+type+fx];
	}

}; // end method
/*____________________________________________________________________________*/


// method: cx_label
/*____________________________________________________________________________*/
AkwaForm.prototype.cx_wrapper = function(fid)
{
	var DivObj = document.createElement('div');
	DivObj.id = fid + '_wrapper';
	return DivObj;
}; // end method
/*____________________________________________________________________________*/


// method: cx_textarea
/*____________________________________________________________________________*/
AkwaForm.prototype.cx_textarea = function(ef_name, ef_label, required, cols, rows)
{
	// create object and set id
	this[ef_name] = document.createElement('textarea');
	this[ef_name].id = ef_name;
	
	// set additional properties
	this[ef_name].required = ( required != null ) ? required : 0;
	this[ef_name].cols = ( cols != null ) ? cols : 40;
	this[ef_name].rows = ( rows != null ) ? rows : 4;
	
	// label
	if ( required ) ef_label += '*';
	this[ef_name].Label = this.cx_label(ef_name, ef_label);
	
	// create wrapper and add label, textarea
	this[ef_name].Wrapper = this.cx_wrapper(ef_name);
	this[ef_name].Wrapper.setAttribute = ('class', 'af_textarea');
	this[ef_name].Wrapper.className = 'af_textarea';
	this[ef_name].Wrapper.appendChild(this[ef_name].Label);
	this[ef_name].Wrapper.appendChild(this[ef_name]);
	
	// add wrapper to form
	this.FormObj.appendChild(this[ef_name].Wrapper);
};
/*____________________________________________________________________________*/


// method: cx_input_text
/*____________________________________________________________________________*/
AkwaForm.prototype.cx_input_text = function(ef_name, ef_label, required, size)
{
	// create object and set id, type
	this[ef_name] = document.createElement('input');
	this[ef_name].id = ef_name;
	this[ef_name].type = 'text';

	// set additional properties
	this[ef_name].required = ( required != null ) ? required : 0;
	this[ef_name].size = ( size != null ) ? size : 8;
	
	// label
	if ( required ) ef_label += '*';
	this[ef_name].Label = this.cx_label(ef_name, ef_label);
	
	// create wrapper and add label, field
	this[ef_name].Wrapper = this.cx_wrapper(ef_name);
	this[ef_name].Wrapper.className = 'af_input_text';
	this[ef_name].Wrapper.appendChild(this[ef_name].Label);
	this[ef_name].Wrapper.appendChild(this[ef_name]);
	
	// add wrapper to form
	this.FormObj.appendChild(this[ef_name].Wrapper);
	
}; // end method
/*____________________________________________________________________________*/


// method: cx_input_checkbox
/*____________________________________________________________________________*/
AkwaForm.prototype.cx_input_checkbox = function(ef_name, ef_label, required, is_checked)
{
	// create object and set id, type
	this[ef_name] = document.createElement('input');
	this[ef_name].id = ef_name;
	this[ef_name].type = 'checkbox';

	// set additional properties
	this[ef_name].required = ( required != null ) ? required : 0;
	this[ef_name].checked = ( is_checked != null && is_checked != 0 && is_checked != false ) ? true : false;
	
	// label
	if ( required ) ef_label += '*';
	this[ef_name].Label = this.cx_label(ef_name, ef_label);
	
	// create wrapper and add label, field
	this[ef_name].Wrapper = this.cx_wrapper(ef_name);
	this[ef_name].Wrapper.className = 'af_input_checkbox';
	this[ef_name].Wrapper.appendChild(this[ef_name].Label);
	this[ef_name].Wrapper.appendChild(this[ef_name]);
	
	// add wrapper to form
	this.FormObj.appendChild(this[ef_name].Wrapper);
}; // end method
/*____________________________________________________________________________*/


// method: cx_input_submit
/*____________________________________________________________________________*/
AkwaForm.prototype.cx_input_submit = function( ef_name, ef_label)
{
	// create object and set id, type
	this[ef_name] = document.createElement('input');
	this[ef_name].id = ef_name;
	this[ef_name].type = 'submit';
	this[ef_name].value = ( ef_label != null ) ? ef_label : 'submit';	
	
	// create wrapper and add label, field
	this[ef_name].Wrapper = this.cx_wrapper(ef_name);
	this[ef_name].Wrapper.className = 'af_input_submit';
	this[ef_name].Wrapper.appendChild(this[ef_name]);
	
	// add wrapper to form
	this.FormObj.appendChild(this[ef_name].Wrapper);
	
}; // end method
/*____________________________________________________________________________*/


// method: cx_input_submit
/*____________________________________________________________________________*/
AkwaForm.prototype.cx_input_reset = function(af_name, af_label, reset_af_id)
{
	// create object and set id, type
	this[af_name] = document.createElement('input');
	this[af_name].id = af_name;
	this[af_name].type = 'reset';
	this[af_name].value = ( af_label != null ) ? af_label : 'reset';
	
	// create wrapper and add label, field
	this[af_name].Wrapper = this.cx_wrapper(af_name);
	this[af_name].Wrapper.className = 'af_input_reset';
	this[af_name].Wrapper.appendChild(this[af_name]);
	
	// add wrapper to form
	this.FormObj.appendChild(this[af_name].Wrapper);
	
	// create reset event
	this[af_name].Target = this[reset_af_id];
	this[af_name].reset_id = function() { this.Target.value = ''; };
	this.attach_event(af_name, 'click', this[af_name].reset_id);	
	
}; // end method
/*____________________________________________________________________________*/


// method: cx_input_submit
/*____________________________________________________________________________*/
AkwaForm.prototype.cx_button = function(ef_name, ef_label)
{
	// create object and set id, type
	this[ef_name] = document.createElement('button');
	this[ef_name].id = ef_name;
	this[ef_name].name = ef_name;
	this[ef_name].innerHTML = ( ef_label != null ) ? ef_label : 'submit';
	this[ef_name].value = this[ef_name].innerHTML;
	
	// create wrapper and add label, field
	this[ef_name].Wrapper = this.cx_wrapper(ef_name);
	this[ef_name].Wrapper.className = 'af_button';
	this[ef_name].Wrapper.appendChild(this[ef_name]);
	
	// add wrapper to form
	this.FormObj.appendChild(this[ef_name].Wrapper);
	
}; // end method
/*____________________________________________________________________________*/


// method: cx_label
/*____________________________________________________________________________*/
AkwaForm.prototype.cx_label = function(fid, text)
{
	LabelObj = document.createElement('label');
	LabelObj.id = fid + '_label';
	LabelObj.setAttribute('for', fid);
	LabelObj.innerHTML = text;
	return LabelObj;
}; // end method
/*____________________________________________________________________________*/


// method: reset_value
/*____________________________________________________________________________*/
AkwaForm.prototype.reset_value = function(fid)
{
	document.getElementById(fid).value = '';
}; // end method
/*____________________________________________________________________________*/


// method: toggle_display
/*____________________________________________________________________________*/
AkwaForm.prototype.toggle_display = function(fid, override)
{
	var obj = document.getElementById(fid);
	
	if ( override != null )
	{
		if ( override ) obj.style.display = '';
		else if ( !override ) obj.style.display = 'none';
	}
	else
	{
		if ( obj.style.display != 'none' ) obj.style.display = 'none';
		else obj.style.display = '';
	}
}; // end method
/*____________________________________________________________________________*/


// method: publish
/*____________________________________________________________________________*/
AkwaForm.prototype.publish = function(pid)
{
	if ( document.getElementById(pid) != null )
	{
		parentObj = document.getElementById(pid);
	}
	else
	{
		alert('id [' + pid + '] not found : will append form to body');
		parentObj = document.body;
	}

	parentObj.appendChild(this.FormObj);
	
}; // end method
/*____________________________________________________________________________*/


// method: compress_html
/*____________________________________________________________________________*/
AkwaForm.prototype.compress_html = function(str, preformatted)
{
	var pre = ( preformatted == null ) ? false : preformatted;
	str = str.replace(/"/g,"&quot;");
	str = str.replace(/<\s*br[^>]*>/g, '\n');
	str = str.replace(/\r\n/g,"<br />");
	str = str.replace(/\n/g,"<br />");
	str = str.replace(/\r/g,"<br />");
	if ( !pre ) str = str.replace(/\s+/g," ");
	if ( pre ) str = "<pre>" + str + "</pre>";
	return str;
}; // end method
/*____________________________________________________________________________*/


// method: escape_html
/*____________________________________________________________________________*/
AkwaForm.prototype.escape_html = function(html)
{
	html = html.replace(/&/g,"&amp;");
	html = html.replace(/\</g,"&lt;");
	html = html.replace(/\>/g,"&gt;");
	return html;	
}; // end method
/*____________________________________________________________________________*/


// method: unescape_html
/*____________________________________________________________________________*/
AkwaForm.prototype.unescape_html = function(html)
{
	html = html.replace(/&lt;/g,"<");
	html = html.replace(/&gt;/g,">");
	html = html.replace(/&amp;/g,"&");
	return html;	
}; // end method
/*____________________________________________________________________________*/

